package com.example.administrator.recyclerviewtest319hz.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.administrator.recyclerviewtest319hz.R;
import com.example.administrator.recyclerviewtest319hz.adapter.MyAdapter;
import com.example.administrator.recyclerviewtest319hz.bean.ArticleBean;
import com.example.administrator.recyclerviewtest319hz.iface.ArticleListener;
import com.example.administrator.recyclerviewtest319hz.model.Articlemodel;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;//显示布局效果
    private MyAdapter myAdapter;//适配器
    private List<ArticleBean> list=null;//数据源

    ArticleListener articleListener= new ArticleListener() {
        @Override
        public void onReaponse(List<ArticleBean> beanlist) {
            list=beanlist;
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();

        //实例化model
        Articlemodel articlemodel = new Articlemodel();
        articlemodel.getResultList("article",2,"rv1l2tidbomann2qn86nln6ur2",articleListener);

    }
    private void initRecyclerView(){
        //获取RecyclerView,设置属性，获取数据源，绑定
        recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
        //创建默认的线性布局
        layoutManager= new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        //list=getData();
        myAdapter=new MyAdapter(this);
        myAdapter.setList(list);

        recyclerView.setAdapter(myAdapter);
    }

    /*
    private List<ArticleBean> getData()
    {
        //定义一个list  赋值  返回
        List<ArticleBean> list = new ArrayList<ArticleBean>();
        ArticleBean articleBean ;

        //第1组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("1");
        articleBean.setDescription("1");
        articleBean.setTime("2017-4-1");
        articleBean.setImgid(R.drawable.f1);
        list.add(articleBean);
        //第2组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(2);
        articleBean.setTitle("2");
        articleBean.setDescription("2");
        articleBean.setTime("2017-4-2");
        articleBean.setImgid(R.drawable.f2);
        list.add(articleBean);
        //第3组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(3);
        articleBean.setTitle("3");
        articleBean.setDescription("3");
        articleBean.setTime("2017-4-3");
        articleBean.setImgid(R.drawable.f3);
        list.add(articleBean);
        //第4组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(4);
        articleBean.setTitle("4");
        articleBean.setDescription("4");
        articleBean.setTime("2017-4-4");
        articleBean.setImgid(R.drawable.f4);
        list.add(articleBean);
        //第5组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(5);
        articleBean.setTitle("5");
        articleBean.setDescription("5");
        articleBean.setTime("2017-4-5");
        articleBean.setImgid(R.drawable.f5);
        list.add(articleBean);
        //第6组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(6);
        articleBean.setTitle("6");
        articleBean.setDescription("6");
        articleBean.setTime("2017-4-6");
        articleBean.setImgid(R.drawable.f6);
        list.add(articleBean);

        return list;
    }*/
}
